from django.db import models

# Create your models here.
from django.db import models
from apps.demo.models import TemporalModel, User


# 新闻频道
class Channel(TemporalModel):
    name = models.CharField(max_length=255, verbose_name='频道名称', unique=True)
    sequence = models.IntegerField(default=0, verbose_name='序号')
    is_visible = models.BooleanField(default=False, verbose_name='是否可见')
    is_default = models.BooleanField(default=False, verbose_name='是否默认')

    # demo = models.ManyToManyField(User, verbose_name='用户关注频', related_name='demo')

    class Meta:
        db_table = 'news_channel'
        verbose_name_plural = '新闻频道'


# 用户关注频道表
class UserChannel(TemporalModel):
    user = models.ForeignKey(User, verbose_name="用户", on_delete=models.CASCADE)
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')
    sequence = models.IntegerField(default=0, verbose_name='序号')
    channel = models.ForeignKey(Channel, on_delete=models.CASCADE, verbose_name='频道ID')

    class Meta:
        db_table = 'news_user_channel'
        verbose_name_plural = '用户关注频道表'


# 文章基本信息表
class Article(TemporalModel):
    STATUS = [(0, '草稿'), (1, '待审核'), (2, '审核通过'), (3, '审核失败'), (4, '已删除'), (5, '封禁')]
    cct = [(1, '喜欢'), (0, '不喜欢'), (-1, '无状态')]
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    channel = models.ForeignKey(Channel, verbose_name='频道ID', on_delete=models.CASCADE)
    title = models.CharField(max_length=100, verbose_name='标题')
    cover = models.JSONField(verbose_name='封面', null=True, blank=True)
    is_advertising = models.BooleanField(default=False, verbose_name='是否投放广告')
    status = models.IntegerField(default=0, choices=STATUS, verbose_name='帖文状态')
    comment_count = models.IntegerField(default=0, verbose_name='评论数')
    allow_comment = models.BooleanField(default=True, verbose_name='是否允许评论')
    reject_reason = models.CharField(max_length=255, verbose_name='驳回原因')
    is_top = models.BooleanField(default=False, verbose_name='是否置顶')


    class Meta:
        db_table = 'news_article_basic'
        verbose_name_plural = "文章基本信息表"


# 文章内容表
class ArticleContent(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name='文章基本信息')
    content = models.TextField(verbose_name='帖文内容')

    class Meta:
        db_table = 'news_article_content'
        verbose_name_plural = '文章内容表'


# 文章统计表
class ArticleStatistic(TemporalModel):
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name='文章ID')
    read_count = models.IntegerField(default=0, verbose_name='阅读量')
    like_count = models.IntegerField(default=0, verbose_name='点赞量')
    dislike_count = models.IntegerField(default=0, verbose_name='不喜欢数')
    repost_count = models.IntegerField(default=0, verbose_name='转发数')
    collect_count = models.IntegerField(default=0, verbose_name='收藏数')
    fans_comment_count = models.IntegerField(default=0, verbose_name='粉丝评论数')

    class Meta:
        db_table = 'news_article_statistic'
        verbose_name_plural = '文章统计表'


# 用户收藏表
class Collection(TemporalModel):
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name='文章ID', on_delete=models.CASCADE)
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = 'news_collection'
        verbose_name_plural = '用户收藏表'


# 用户阅读历史表
class Read(TemporalModel):
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name='文章ID', on_delete=models.CASCADE)

    class Meta:
        db_table = 'news_read'
        verbose_name_plural = '用户阅读历史表'


# 用户文章态度表
class Attitude(TemporalModel):
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name='文章ID', on_delete=models.CASCADE)
    attitude = models.IntegerField(default=-1, choices=[(0, '不喜欢'), (1, '点赞')], verbose_name='态度')

    class Meta:
        db_table = 'news_attitude'
        verbose_name_plural = '用户文章态度表'


# 文章举报
class Report(TemporalModel):
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name='文章ID', on_delete=models.CASCADE)
    type = models.IntegerField(default=0, verbose_name='问题类型')
    remark = models.CharField(max_length=100, verbose_name='备注问题')

    class Meta:
        db_table = 'news_report'
        verbose_name_plural = '文章举报'


# 文章评论
class Comment(TemporalModel):
    STATUS = [(0, '待审核'), (1, '审核通过'), (2, '审核失败'), (3, '已删除')]
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name='文章ID', on_delete=models.CASCADE,null=True)
    parent = models.ForeignKey("self", verbose_name='被评论的评论id', on_delete=models.CASCADE,null=True)
    like_count = models.IntegerField(default=0, verbose_name='点赞数')
    reply_count = models.IntegerField(default=0, verbose_name='回复数')
    content = models.CharField(max_length=255, verbose_name='评论内容')
    is_top = models.BooleanField(default=False, verbose_name='是否置顶')
    status = models.IntegerField(choices=STATUS, default=0, verbose_name='评论状态')

    class Meta:
        db_table = 'news_comment'
        verbose_name_plural = '文章评论'


# 评论点赞
class CommentLiking(TemporalModel):
    user = models.ForeignKey(User, verbose_name='用户ID', on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, verbose_name='评论ID')
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = 'news_comment_liking'
        verbose_name_plural = '评论点赞'
